package com.gy.smart.base;

import android.graphics.Typeface;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.smart.R;
import com.gy.smart.util.FragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2016/7/7.
 * Describe:
 */
public class MineBaseActivity extends BaseActivity {

    private ViewPager viewPager;
    private LinearLayout llItem1,llItem2;
    private LinearLayout view_head;
    private TextView tvItem1,tvItem2;
    private View lineItem1,lineItem2;
    private TextView tv_integral;
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentAdapter fragmentAdapter;
    private PageSelected pageSelected;
    public int position = 0;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_mine_base);
        view_head = (LinearLayout) findViewById(R.id.linear_mine_head);
        viewPager = (ViewPager) findViewById(R.id.activity_mine_base_viewpager);
        llItem1 = (LinearLayout) findViewById(R.id.activity_mine_base_ll_item1);
        llItem2 = (LinearLayout) findViewById(R.id.activity_mine_base_ll_item2);
        tvItem1 = (TextView) findViewById(R.id.activity_mine_base_tv_item1);
        tvItem2 = (TextView) findViewById(R.id.activity_mine_base_tv_item2);
        lineItem1 = findViewById(R.id.activity_mine_base_line_item1);
        lineItem2 = findViewById(R.id.activity_mine_base_line_item2);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        super.setListener();
        llItem1.setOnClickListener(this);
        llItem2.setOnClickListener(this);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MineBaseActivity.this.position = position;
                setSelect(position);
                if(pageSelected != null){
                    pageSelected.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.activity_mine_base_ll_item1:
                setSelect(0);
                viewPager.setCurrentItem(0);
                break;
            case R.id.activity_mine_base_ll_item2:
                setSelect(1);
                viewPager.setCurrentItem(1);
                break;
        }
    }

    private void setSelect(int item){
        if(item == 0){
            tvItem1.setTextColor(this.getResources().getColor(R.color.color_333333));
            tvItem2.setTextColor(this.getResources().getColor(R.color.black));
            lineItem1.setBackgroundColor(getResources().getColor(R.color.title_red));
            lineItem2.setBackgroundColor(getResources().getColor(R.color.white));
            tvItem1.setTypeface(Typeface.DEFAULT_BOLD);
            tvItem2.setTypeface(Typeface.DEFAULT);
        } else if(item == 1){
            tvItem1.setTextColor(this.getResources().getColor(R.color.black));
            tvItem2.setTextColor(this.getResources().getColor(R.color.color_333333));
            lineItem1.setBackgroundColor(getResources().getColor(R.color.white));
            lineItem2.setBackgroundColor(getResources().getColor(R.color.title_red));
            tvItem1.setTypeface(Typeface.DEFAULT);
            tvItem2.setTypeface(Typeface.DEFAULT_BOLD);
        }
    }

    public void setFragments(List<Fragment> fragments){
        this.fragments = fragments;
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(fragmentAdapter);
    }

    public void setPageSelected(PageSelected pageSelected){
        this.pageSelected = pageSelected;
    }

    /** 设置标签名字*/
    public void setItemName(String item1,String item2){
        tvItem1.setText(item1);
        tvItem2.setText(item2);
    }

    /** 添加布局*/
    public void setView(View view){
        if(view != null){
            view_head.addView(view);
        }
    }

    public interface PageSelected{
        void onPageSelected(int position);
    }
}
